package daoIpmt;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import beans.Likes;
import beans.PersonInformation;
import dao.LikesDao;
import utils.DataSourceUtils;

public class LikesDaoIpmt implements LikesDao {
	QueryRunner qu = new QueryRunner(DataSourceUtils.getDataSource());

	@Override
	public void addLiker(Likes likes) {
		
		
		String sql1="select * from likes where username = ? and likeusername=?";
		Likes likes2;
		try {
			likes2 = qu.query(sql1,new BeanHandler<Likes>(Likes.class),likes.getUsername(),likes.getLikeusername());
			if(likes2 == null) {
				likes.setStatus(1);
				String sql2="insert into likes values(?,?,?)";
				qu.update(sql2,likes.getUsername(),likes.getLikeusername(),likes.getStatus());
			}else {
				if(likes.getStatus()==1) {
					String sql4="update likes set status=? where username=? and likeusername=?";
					likes.setStatus(0);
					qu.update(sql4,likes.getStatus(),likes.getUsername(),likes.getLikeusername());
				}else {
					String sql3="update likes set status=? where username=? and likeusername=?";
					likes.setStatus(1);
					qu.update(sql3,likes.getStatus(),likes.getUsername(),likes.getLikeusername());
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
	}

	@Override
	public void getStatus(Likes likes) {
		String sql = "select * from likes where username = ? and likeusername=?";
		try {
			Likes likes2 = qu.query(sql,new BeanHandler<Likes>(Likes.class),likes.getUsername(),likes.getLikeusername());
			if(likes2 != null) {
			likes.setStatus(likes2.getStatus());
			}else {
				likes.setStatus(0);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

}
